/**
 * @file 
 * @description 公共库
 */

/**
 * @method
 * @description 深拷贝
 * @param {object} source 源数据（要拷贝的对象）
 */
export const deepClone = (source) => {
    const dest = Array.isArray(source) ? [] : {}; //目标对象
    /**
     * @param {Object} dst 目标数据 (要拷贝到的对象上)
     * @param {Object} sour 源数据 (要拷贝的对象)
     */
    const deep = (dst, sour) => {
        for (const key in sour) {
            if (Object.hasOwnProperty.call(sour, key)) {
                const item = sour[key];

                if (item instanceof RegExp) {
                    dst[key] = new RegExp(item)
                } else if (item instanceof Date) {
                    dst[key] = new Date(item)
                } else if (typeof item === 'function') {
                    dst[key] = item
                }


                if (item instanceof Object) {
                    deep(dst[key] = Array.isArray(item) ? [] : {}, item)
                } else {
                    dst[key] = item;
                }
            }
        }
    }
    deep(dest, source)

    return dest
}


/**
 * @methods
 * @description 下载文件流
 * @param {Blob} data 数据
 * @param {string} fileName 文件名
 */

export const download = (data, fileName) => {
    let blob = new Blob([data], {
        type: "application/vnd.ms-excel"
    });
    let downloadElement = document.createElement("a");
    let href = window.URL.createObjectURL(blob); //创建下载链接
    downloadElement.href = href;
    downloadElement.download = decodeURIComponent(fileName); //解码
    document.body.appendChild(downloadElement);
    downloadElement.click();
    document.body.removeChild(downloadElement);
    window.URL.revokeObjectURL(href); //释放掉blob对象
}



